package jp.co.nes.awssupport.compile.function;

import java.time.YearMonth;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jp.co.nes.awssupport.access.dao.DailyCompiledBillingDao;
import jp.co.nes.awssupport.access.dao.DailyCompiledBillingDao.DaoReadType;
import jp.co.nes.awssupport.business.base.AbstractRequestWithCompletionStateHandler;
import jp.co.nes.awssupport.business.bean.CommonContainerBean.ProcessingResult;
import jp.co.nes.awssupport.business.bean.CompileContainerBean;
import jp.co.nes.awssupport.business.bean.CompileInfoBean;
import jp.co.nes.awssupport.business.logic.MonthlyCompiledBillingProcessing;
import jp.co.nes.awssupport.common.bean.DailyCompiledBillingBean;
import jp.co.nes.awssupport.common.dao.ReadHandler;
import jp.co.nes.awssupport.common.dao.ReadType;
import jp.co.nes.awssupport.common.utility.AppInfoUtil;
import jp.co.nes.awssupport.common.utility.BeanUtil;
import jp.co.nes.awssupport.common.utility.DateUtil;

/**
 * 月次集計結果出力(ボリューミングプラン適用)です(Lambda)。
 */
public class MonthlyCompiledBilling
        extends AbstractRequestWithCompletionStateHandler<CompileInfoBean, CompileContainerBean<CompileInfoBean>> {

    /** ロガー */
    private static Logger logger = LoggerFactory.getLogger(MonthlyCompiledBilling.class);

    /**
     * デフォルトコンストラクタです。
     */
    public MonthlyCompiledBilling() {
    }

    /*
     * (非 Javadoc)
     * @see jp.co.nes.awssupport.common.base.AbstractRequestHandler#handleRequest(java.lang.Object)
     */
    @Override
    protected CompileContainerBean<CompileInfoBean> handleRequest(CompileInfoBean input) {
        String category = "MonthlyCompiledBilling";
        String defKey = AppInfoUtil.getVal(category, "DefaultRegionCodeKey", "EmptyRegionCode");
        int max = AppInfoUtil.getIntValAndMinChk(category, "BatchExecuteMaxCount", 5000, 1);

        if (logger.isDebugEnabled()) {
            logger.debug(category + ",DefaultRegionCodeKey=" + defKey);
            logger.debug(category + ",BatchExecuteMaxCount=" + max);
        }

        YearMonth targetYearMonth = DateUtil.toYearMonth(input.getTargetYearMonthValue());
        String linkedAccountId = input.getLinkedAccountId();

        // 検索条件の作成
        Object[] conditions = new Object[2];
        conditions[0] = linkedAccountId;
        conditions[1] = DateUtil.toInt(targetYearMonth);

        MonthlyCompiledBillingProcessing mcbProcessing = new MonthlyCompiledBillingProcessing(targetYearMonth, linkedAccountId);
        mcbProcessing.delete();

        Class<DailyCompiledBillingBean> cls = DailyCompiledBillingBean.class;
        ReadType readType = DaoReadType.TOTALIZATION;
        DailyCompiledBillingDao dao = new DailyCompiledBillingDao();
        boolean isExist = dao.read(new ReadHandler<DailyCompiledBillingBean>(cls, readType) {
            /*
             * (非 Javadoc)
             * @see jp.co.nes.awssupport.common.dao.ReadHandler#read(jp.co.nes.awssupport.common.bean.BaseBean)
             */
            @Override
            protected void read(DailyCompiledBillingBean dcbBean) {
                mcbProcessing.add(dcbBean);
            }
        }, conditions);

        if (isExist) {
            // 日次集計結果が存在した場合、以下の処理を実施(存在しなかった場合は、何も実施しない)
            mcbProcessing.insert(max);
        }

        // 返却内容の設定
        CompileInfoBean info = new CompileInfoBean();
        BeanUtil.copyProperties(info, input);
        CompileContainerBean<CompileInfoBean> output = new CompileContainerBean<>();
        output.setInfoBean(info);
        output.setProcessingResult(ProcessingResult.SUCCESS);
        return output;
    }
}
